<template>
  <PageBox :data="dataInfo" @searchBtn="getTbody" :maring="maring" :isRadio="isRadio" :isCheckbox="isCheckbox"
    @tableCheckboxBtn="tableCheckboxBtn" @tableRadioBtn="tableRadioBtn">
    <template #search="{ search }">
      <ElFormItemBox label="结存单号" prop="likeWords">
        <ElInputBox :data="search" dataName="likeWords" />
      </ElFormItemBox>
    </template>
    <template #headR>
    </template>
  </PageBox>
  <ElPupFormBox :pup="pup">
    <template #default="{ form }">
      <ElFormItemBox label="结存日期" prop="excuteDate" isRules>
        <ElDayBox :data="form" dataName="excuteDate" :disabled-date="(time: Date) => {
          const tbody: any = dataInfo.table.tbody
          const startDate = tbody?.length > 0 ? new Date(tbody[0].startDate) : null // 可选开始日期
          const endDate = new Date()   // 可选结束日期
          if (startDate) {
            return time.getTime() < startDate.getTime() || time.getTime() > endDate.getTime()
          } else {
            return time.getTime() > endDate.getTime()
          }
        }" @changeBtn="(value: any) => {
          if (value) {
            const tbody: any = dataInfo.table.tbody
            const startDate = tbody?.length > 0 ? new Date(tbody[0].startDate) : null // 可选开始日期
            const endDate = value// 可选开始日期
            form.stockBalanceRemark = `${proxy.$day(startDate)} 到 ${proxy.$day(endDate)}结存`
          } else {
            form.stockBalanceRemark = ''
          }

        }" />
      </ElFormItemBox>
      <ElFormItemBox label="结存备注" prop="stockBalanceRemark" isRules class="wAuto">
        <ElInputBox :data="form" dataName="stockBalanceRemark" type="textarea" />
      </ElFormItemBox>
    </template>
  </ElPupFormBox>
</template>
<script setup lang="ts">
const { proxy } = getCurrentInstance() as any;
import './pageCss.scss'; // 引入样式
import { useListComponent } from './pageTs'; // 引入逻辑
const props = withDefaults(
  defineProps<{
    isRadio?: boolean; //单选
    isCheckbox?: boolean; //多选
    maring?: number; //间距
    query?: any; //
  }>(),
  {
  },
);
const pup = reactive({
  state: false,
  typeName: 0,
  title: `结存管理`,
  width: 600,
  height: 'auto',
  form: {
    warehouseCode: '',//结存仓库代码
    excuteDate: '',//结存日期\n"2024-11-26",//需要准确到日期，前段需要限制不能超过当天日期
    stockBalanceRemark: '',//结存备注
  },
  btns: [
    {
      title: '确认',
      type: 'primary',
      click: (pup: any) => {
        pup.state = true;
        proxy.$api.StockBalance.JuadgeIsHaveStockBalanceBill(pup.form)
          .then((res: any) => {
            if (res.data) {
              proxy.$message('该仓库该日期存在结存单，不可多次创建结存单', 'error');
              pup.loading = false;
            } else {
              proxy.$api.StockBalance.CreateStockBalanceBill(pup.form)
                .then((res: any) => {
                  if (res) {
                    proxy.$message('创建成功');
                    pup.state = false;
                    getTbody()
                  } else {

                  }
                })
                .catch(() => {
                  pup.loading = false;
                });
            }
          })
          .catch(() => {
            pup.loading = false;
          });
      }
    }
  ]
});
const emit = defineEmits<{
  //fn为父组件引入子组件是定义的子组件事件属性，可随意定义
  (event: 'tableRadioBtn', row: any): void;
  (event: 'tableCheckboxBtn', arr: any): void;
}>();
//多选数据
const tableCheckboxBtn = (arr: any) => {
  emit('tableCheckboxBtn', arr);
};
//单选数据
const tableRadioBtn = (row: any) => {
  emit('tableRadioBtn', row);
};

// 组件挂载后执行
onMounted(() => {
  getTbody()
});
const { dataInfo, getTbody, options } = useListComponent({
  ...props,
  pup: pup
});
</script>
